/*
 * webpack基础配置
 * @author li.xiaomei
 * @date 2016-12-19
 */
var path = require('path')

var entryFilesFn = require('./module/entryFn.js')
var htmlWebpackFn = require('./module/htmlWebpackFn.js')
var commonMethod = require('../method.js')

//全局配置
var global_config = global.think_config,
    webpack_config = global_config.webpack,

    think_dir = __dirname,
    //解析为绝对路径
    excludeDir = path.resolve(think_dir, '../../../node_modules');

//webpack中涉及目录配置最好使用绝对路径
module.exports = function(env){
  //dev环境时，不需要publicPath参数配置，默认是根目录
  var output = {
        //指定输出文件路径
        path: webpack_config.assetsRoot,
        //设置想要访问的资源路径，
        //访问时，需要通过类似http://localhost:8080/assets/index-1.js来访问资源
        //如果不设置，则默认从站点根目录加载
        //publicPath: webpack_config.assetsPublicPath,
        //输出文件名称 （[id]chunk的id、[name]chunk名、[hash]编译哈希值、[chunkhash]）
        filename: webpack_config.filename
  };
  if(env!='dev') {
      output.publicPath = webpack_config.assetsPublicPath;
  }

  return {
    //上下文,待解析文件的上下文
    //context: excludeDir,
    entry: entryFilesFn(env),

    output: output,

    //文件路径的指向
    //配置应用层的模块（要被打包的模块）解析
    resolve: {
      //modules: [path.resolve(process.cwd(), "app"), "node_modules"], //应用层模块将按照这里指定的所有目录搜索
      root: excludeDir,
      fallback: [excludeDir, process.cwd()],
      //当requrie的模块找不到时，添加这些后缀
      //该值定义解析模块时的查找文件的后缀顺序
      extentions: webpack_config.resolve.extentions,
      alias: webpack_config.resolve.alias
    },

    //配置loader模块的解析
    resolveLoader: {
        //webpack2配置
        modules: [excludeDir],
        // root: excludeDir,
        // fallback: [excludeDir],
        // webpack1配置
        modulesDirectories: [excludeDir]
    },

    //配置.要使用的loader
    //把资源文件（css、图片、html等模块）处理成相应的js模块，
    //然后其他plugins才能对资源进一步处理
    module: {
      loaders: [
      {
          test: /\.vue$/,
          loader: 'vue-loader',
          //include: process.cwd(),
          exclude: /node_modules/
      },
      {
          test: /\.json$/,
          loader: 'json-loader'
      },
      {
          test: /\.html$/,
          loader: 'html-loader'
      },
      {
          test: /\.js$/,
          loader: 'babel-loader',
          include: [process.cwd(), excludeDir],
          exclude: /node_modules/,
          query: {
            //这个之后得改，这里因为presets和plugins默认的查询路径和待编译文件是同一个目录的
            //暂时没有想到更好的办法
            //目前是在babel-core下面option-manager： dirname = _ref2.options.sourceRoot || _ref2.dirname;
            sourceRoot: excludeDir,
            plugins: ['transform-runtime'],
            presets: ['es2015', 'stage-2','react']
          }
      },{
          test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
          loader: 'url-loader',
          query: {
              limit: 10,
              name: '[name].[ext]'
          }
      }
      ],

      resolve: {
        modulesDirectories: [excludeDir, 'node_modules']
      }
    },
    //vue里的css也要单独提取出来
    vue: {
        loaders: commonMethod.cssLoaders({ sourceMap: true })
    },
    // babel: {
    //   presets: ['es2015', 'stage-2'],
    //   plugins: ['transform-runtime']
    // },

    //比loader功能更加强大的插件
    plugins: htmlWebpackFn(env)
  }

}
